package com.imooc.business.core.executor.query.base.table;

import com.imooc.business.core.executor.query.base.AbstractTableService;
import com.imooc.module.query.enums.OrganType;
import com.imooc.module.query.enums.TableType;
import com.imooc.module.query.request.QueryRequest;
import com.imooc.module.query.request.SupportType;
import com.imooc.module.query.response.QueryDataResponse;
import com.imooc.user.provider.model.UserCenterGroupVo;
import com.imooc.user.provider.model.UserCenterUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Tim
 * @description: 用户报表数据
 */
@Slf4j
@Repository
public class UserTableService extends AbstractTableService<UserCenterUserVo,Long> {

    /**
     * 获取当前群组下面所有的用户
     * @param request
     * @return
     */
    protected List<UserCenterUserVo> getOrganList(QueryRequest request){
        UserCenterGroupVo group = request.getGroup();
        List<UserCenterUserVo> relations = userService.getUsersByGroupId(group.getGroupId());
        return relations;
    }

    protected Map<Object, Map<String, Object>> getDataMap(QueryRequest request, List<UserCenterUserVo> relations){
        request.setUserIds(relations.stream().map(UserCenterUserVo::getId).collect(Collectors.toList()));
        //查询当前群组下所有用户数据
        Map<Object, Map<String, Object>> userMap = execQuery(request);
        return userMap;
    }

    @Override
    protected void setSumaryAndClickAble(QueryDataResponse.TableData tableData, UserCenterUserVo user) {
        tableData.setIsSummary(false);
        tableData.setClickAble(false);
    }

    @Override
    protected String getName(UserCenterUserVo user) {
        return user.getUserName();
    }

    @Override
    protected Long getOrganId(UserCenterUserVo user) {
        return user.getId();
    }

    @Override
    public Boolean supportType(SupportType supportType) {
        return OrganType.USER.equals(supportType.getOrganType()) &&
                TableType.TABLE.equals(supportType.getTableType());
    }
}
